package com.kgc.easyshopping.service.Impl;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.kgc.easyshopping.dao.OrderMapper;
import com.kgc.easyshopping.entity.Order;
import com.kgc.easyshopping.service.DetailService;
import com.kgc.easyshopping.service.OrderService;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.List;
import java.util.Map;
@Service
public class OrderServiceImpl implements OrderService {
    @Resource
    private OrderMapper orderMapper;
    @Resource
    private DetailService detailService;

    @Override
    public PageInfo<Order> selectPageInfo(Map<String, Object> params) {
        int pageNum = (int) params.get("pageNum");
        int pageSize = (int) params.get("pageSize");
        PageHelper.startPage(pageNum,pageSize);
        List<Order> orders = orderMapper.selectAll(params);
        for(Order order:orders){
            order.setDetails(detailService.selectByOrderId(order.getOid()));
        }
        return new PageInfo<Order>(orders,5);
    }

    @Override
    public PageInfo<Order> selectUserOrder(Map<String, Object> params) {
        int pageNum = (int) params.get("pageNum");
        int pageSize = (int) params.get("pageSize");
        String userid = (String) params.get("userid");
        PageHelper.startPage(pageNum,pageSize);
        List<Order> orders = orderMapper.selectUserOrder(userid);
        for(Order order:orders){
            order.setDetails(detailService.selectByOrderId(order.getOid()));
        }
        return new PageInfo<Order>(orders,5);
    }

    @Override
    public boolean updateByPrimaryKeySelective(Order order) {
        return orderMapper.updateByPrimaryKeySelective(order)==1?true:false;
    }

    @Override
    public Order selectBySerialnumber(String serialnumber) {
        return orderMapper.selectBySerialnumber(serialnumber);
    }
}
